//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.quic.quiche.server;

import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.util.Set;

import org.eclipse.jetty.quic.quiche.PemExporter;
import org.eclipse.jetty.quic.quiche.PemPaths;
import org.eclipse.jetty.quic.server.ServerQuicConfiguration;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>Quiche specific {@link ServerQuicConfiguration}.</p>
 * <p>The PEM working directory constructor argument is mandatory, although
 * it may be set after construction via {@link #setPemWorkDirectory(Path)}
 * before starting this instance.</p>
 */
public class QuicheServerQuicConfiguration extends ServerQuicConfiguration
{
    private static final Logger LOG = LoggerFactory.getLogger(QuicheServerQuicConfiguration.class);

    private Path pemWorkDirectory;

    public QuicheServerQuicConfiguration()
    {
        this(null);
    }

    public QuicheServerQuicConfiguration(Path pemWorkDirectory)
    {
        this.pemWorkDirectory = pemWorkDirectory;
    }

    public Path getPemWorkDirectory()
    {
        return pemWorkDirectory;
    }

    public void setPemWorkDirectory(Path pemWorkDirectory)
    {
        if (isStarted())
            throw new IllegalStateException("cannot change PEM working directory after start");
        this.pemWorkDirectory = pemWorkDirectory;
    }

    public void configure(SslContextFactory.Server sslContextFactory) throws Exception
    {
        Path pemWorkDirectory = getPemWorkDirectory();
        Set<String> aliases = sslContextFactory.getAliases();
        if (aliases.isEmpty())
            throw new IllegalStateException("Missing or invalid KeyStore: a SslContextFactory configured with a valid, non-empty KeyStore is required");
        String alias = sslContextFactory.getCertAlias();
        if (alias == null)
            alias = aliases.stream().findFirst().orElseThrow();
        String keyManagerPassword = sslContextFactory.getKeyManagerPassword();
        char[] password = keyManagerPassword == null ? sslContextFactory.getKeyStorePassword().toCharArray() : keyManagerPassword.toCharArray();
        KeyStore keyStore = sslContextFactory.getKeyStore();
        Path[] keyPair = PemExporter.exportKeyPair(keyStore, alias, password, pemWorkDirectory);
        Path privateKeyPemPath = keyPair[0];
        Path certificateChainPemPath = keyPair[1];
        Path trustedCertificatesPemPath = null;
        KeyStore trustStore = sslContextFactory.getTrustStore();
        if (trustStore != null)
            trustedCertificatesPemPath = PemExporter.exportTrustStore(trustStore, pemWorkDirectory);
        getImplementationConfiguration().put(sslContextFactory, new PemPaths(privateKeyPemPath, certificateChainPemPath, trustedCertificatesPemPath));
    }

    public void deconfigure(SslContextFactory.Server sslContextFactory)
    {
        PemPaths pemPaths = (PemPaths)getImplementationConfiguration().remove(sslContextFactory);
        if (pemPaths != null)
        {
            deleteFile(pemPaths.privateKeyPemPath());
            deleteFile(pemPaths.certificateChainPemPath());
            deleteFile(pemPaths.trustedCertificatesPemPath());
        }
    }

    private void deleteFile(Path path)
    {
        try
        {
            if (path != null)
                Files.delete(path);
        }
        catch (Throwable x)
        {
            if (LOG.isDebugEnabled())
                LOG.atDebug().setCause(x).log("could not delete {}", path);
        }
    }
}
